package com.guqj.redis.subscribe;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.junit.Test;

import redis.clients.jedis.Jedis;

public class RedisPubSubTest {

	private final static ThreadLocal<SimpleDateFormat> df = new ThreadLocal<SimpleDateFormat>() {
		@Override
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		}

	};

	@Test
	public void sub() {
		Jedis jedis = null;
		try {
			jedis = new Jedis("127.0.0.1", 6379, 1);
			RedisMsgPubSubListener redisPub = new RedisMsgPubSubListener();
			jedis.subscribe(redisPub, "news.share", "news.blog"); // subscribe是一个阻塞的方法，在取消订阅该频道前，会一直阻塞在这，只有当取消了订阅才会执行下面的程序
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != jedis) {
				jedis.disconnect();
			}
		}
	}

	@Test
	public void publish() {
		Jedis jedis = null;
		try {
			jedis = new Jedis("127.0.0.1", 6379, 1);
			jedis.publish("news.share", "我是天才");
			Thread.sleep(5000);
			jedis.publish("news.blog", "哈哈哈哈哈哈");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != jedis) {
				jedis.disconnect();
			}
		}
	}

	@Test
	public void test() {

		for (int i = 0; i < 10; i++) {
			System.out.println("~~~~~~~~~~~ 第 " + i + " ci ~~~~~~~~~~~~~~~~");
			Set<String> set = new HashSet<>();
			set.add("PHP");
			set.add("Python");
			set.add("Ruby");
			set.add("Java");
			set.add("Erlang");
			set.add("Golang");
			System.out.println("Set Keys:" + set);
			for (Iterator<String> it = set.iterator(); it.hasNext();) {
				System.out.print(it.next() + " ");
			}
			System.out.println(" ");
			System.out.println("-------------------------");
			System.out.println("Golang:" + hash("Golang"));
			System.out.println("Java:" + hash("Java"));
			System.out.println("PHP:" + hash("PHP"));
			System.out.println("Erlang:" + hash("Erlang"));
			System.out.println("Ruby:" + hash("Ruby"));
			System.out.println("Python:" + hash("Python"));
			System.out.println("==========================");
			Map<String, String> map = new HashMap<>();
			System.out.println(map.put("PHP", "PHP"));
			System.out.println(map.put("Python", "Python"));
			System.out.println(map.put("Java", "Java"));
			System.out.println(map.put("Ruby", "Ruby"));
			System.out.println(map.put("Golang", "Golang"));
			System.out.println(map.put("Erlang", "Erlang"));
			System.out.println("Map Keys:" + map);
			
			for (Iterator<String> it = map.keySet().iterator(); it.hasNext();) {
				System.out.print(map.get(it.next()) + " ");
			}
			System.out.println(" ");
			System.out.println("~~~~~~~~~~~~~~~~~~~~~~~");
			for (Map.Entry<String, String> m : map.entrySet()) {
				System.out.print(m.getValue() + " ");
			}
		}
	}

	public int hash(Object key) {
		int h;
		return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
	}

	@Test
	public void testHash() {
		System.out.println(1 >> 16);
	}
}
